<template>
  <echartBox title="工程统计" width="651px" height="451px">
    <div class="echart-show" ref="chart"></div>
  </echartBox>
</template>

<script>
import * as echarts from "echarts";
import echartBox from "../echart-box.vue";

export default {
  name: "Page1ItemBox5",
  components: {
    echartBox,
  },
  props: {
    chartData: {
      type: Array,
      required: true,
      default: () => [],
    },
  },
  methods: {
    // 初始化图表的统一方法名
    initChart() {
      this.buildProjectStatChart();
    },
    // 构建工程统计图表
    buildProjectStatChart() {
      let _labelList = this.chartData.map((item) => item.name);
      let _valueList = this.chartData.map((item) => item.value);
      let chartContainer = this.$refs.chart;
      let myChart = echarts.init(chartContainer);
      let barWidth = 27;

      const option = {
        tooltip: {
          show: true,
          trigger: "axis", //axis , item
          backgroundColor: "RGBA(0, 49, 85, 1)",
          borderColor: "rgba(0, 151, 251, 1)",
          borderWidth: 1,
          borderRadius: 0,
          textStyle: {
            color: "#BCE9FC",
            fontSize: 16,
            align: "left",
          },
        },
        backgroundColor: "transparent",
        grid: {
          left: "8%",
          right: "1%",
          bottom: "1%",
          top: "16%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          data: _labelList,
          axisLine: {
            show: true,
            lineStyle: {
              color: "rgba(255, 255, 255, 0.3)",
            },
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            color: "#fff",
            fontSize: 15,
          },
        },
        yAxis: {
          name: "单位：万/m³",
          nameTextStyle: {
            color: "#fff",
          },
          type: "value",
          max: 600,
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "rgba(255, 255, 255, 0.1)",
              type: "dashed",
            },
          },
          axisLabel: {
            color: "#fff",
            fontSize: 15,
          },
        },
        series: [
          {
            name: "",
            tooltip: {
              show: true,
            },
            type: "bar",
            barWidth: barWidth,
            label: {
              show: false,
            },
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "#21daf6",
                },
                {
                  offset: 1,
                  color: "#21daf6",
                },
              ]),
            },
            data: _valueList,
            legendHoverLink: false,
          },
          {
            // 柱子底部椭圆
            name: "",
            tooltip: {
              show: false,
            },
            type: "pictorialBar",
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "#21daf6",
                },
                {
                  offset: 1,
                  color: "#21daf6",
                },
              ]),
            },
            symbolRotate: 0,
            symbolSize: [barWidth, 6],
            symbolOffset: [0, 3],
            symbolPosition: "start",
            data: _valueList,
            z: 3,
          },
          {
            // 柱子头部椭圆
            name: "",
            tooltip: {
              show: false,
            },
            type: "pictorialBar",
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "#1ab1c8",
                },
                {
                  offset: 1,
                  color: "#1ab1c8",
                },
              ]),
            },
            symbolRotate: 0,
            symbolSize: [barWidth, 6],
            symbolOffset: [0, -3],
            symbolPosition: "end",
            data: _valueList,
            z: 3,
          },
        ],
      };

      myChart.setOption(option);
    },
  },
};
</script>

<style scoped lang="scss">
.echart-show {
  width: 100%;
  height: 100%;
}
</style>
